import { utilJs } from "@/utils/common.js";
export const memberIndexMixin = {
  data() {
    return {
      theme: localStorage.getItem("colorName") ? localStorage.getItem("colorName") : 'mb5_default',
      hideRecord: true,
      companyAuth: null,
      memberInfo: {}, // 会员信息
      questionInfo: {},
      vipType: "", // 会员身份名
      vipTypeNum: null,
      svipEndTime: '', //svip到期时间
      vipEndTime: '', //vip到期时间
      isShowSign: true, // 是否显示签到
      isShowRenew: false, // 是否显示续费
      isiOS: false,
      isYouke: false,
      isAnApp: false,
      exchangeText: '优惠券',
      modeleMapInfo: null,
      recId: 0,
      recDec: '',
      balance: '',
      enableIosRecharge: false, //是否开启ios充值
      isShowMask: false,
      enableGiftCard: true, // 是否开启虚拟币充值
      isApp: false, // 是否是app,
      baseConf: {}, //基本信息
      personalModuleList: {}, //模块总的信息
      giftCardName: '',
      promotedAccount: {}, //推广账号
      showPresent: false, // 是否展示会员礼包
      addMemberGift: {}, // 会员礼包详情
      assetManagement: null, // 资产管理展示数据
      isShowStudy: true, // 是否展示学习中心
      isShowOrder: true, // 是否展示订单管理
      showShopDetail: false //是否展示店铺信息
    }
  },
  created() {
  },
  computed: {
    // 判断是否展示签到
    showSignIn() {
      return !(this.isiOS || this.isAnApp || this.$isWxApp() || this.$isStore()) && this.baseConf && this.baseConf.showSign;
    },
    isShowMember() {
      // iosapp 未开启购买会员, 取反 不显示会员升级入口.其他端 判断类似  小程序ios环境固定不展示会员信息
      let isShowMember = this.isiOS ? !(this.isiOS && this.enableIosRecharge == false) : !(this.baseConf.showVip == 0)
      return isShowMember && !this.$isWxAppIos()
    },
    assetManagementShowItem() {
      return function(type) {
        if (!this.assetManagement || (this.assetManagement && !this.assetManagement.project)) return true
        return this.assetManagement.project.find(item => item.keyword == type && item.enable == 1) ? true : false
      }
    }
  },
  mounted() {
  },
  activated() {
    this.mx_initData();
    this.mx_getMemberIndexModuleInfo();
    this.mx_getMemberInfo();
    this.mx_getCardName();
    this.mx_getShopDetail();
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    iosAppRouteChange(toPath) {
      if (utilJs.isIOSApp()) {
        window.webkit.messageHandlers.iosWechatRoute.postMessage(toPath);
      } else if (utilJs.isAndoridNewApp()) {
        let data = {
          path: toPath
        }
        window.androidWechatRoute.doAction(JSON.stringify(data))
      } else {
        this.$routerGo(this, "push", { path: toPath });
      }
    },
    //ios退出
    iosLogoOut() {
      window.localStorage.clear();
      if (utilJs.isIOSApp()) {
        window.webkit.messageHandlers.iosWechatLoginOut.postMessage('loginOut');
      } else {
        utilJs.androidAppExit();
        let companyId = localStorage.getItem("companyId");
        let urlS = window.location.href.split(companyId)[0] + companyId;
        window.location.href = `${urlS}/?#/homePage/login/iosBindMobile`;
      }
    },
    copyId() {
      if (!this.memberInfo.showId) return;
      utilJs.copyText(this.memberInfo.showId);
    },
    goRecharge() {
      let toPath = '/homePage/course/iosRecharge';
      this.iosAppRouteChange(toPath);
    },
    //点击退货
    clickRefund() {
      let toPath = '/member/myRefund/myRefundList';
      this.iosAppRouteChange(toPath);
    },
    //ios绑定手机号
    goIosBindMobile() {
      let toPath = '/homePage/login/iosBindMobile';
      this.iosAppRouteChange(toPath);
    },
    //学习中心
    clickLearnCenter() {
      let toPath = '/member/watchList/learnCenter';
      this.iosAppRouteChange(toPath);
    },
    clickPersonalMessageSetting(e) {
      //推送开关设置
      let toPath = '/member/personalMessageSetting/personalMessageSetting';
      this.iosAppRouteChange(toPath);
    },
    clickSign(e) {
      //点击签到
      let toPath = '/member/myIntegral/integralIndex';
      this.iosAppRouteChange(toPath);
    },
    clickAvatar(e) {
      //点击头像
      let toPath = '/member/menberInfo/infoIndex';
      this.iosAppRouteChange(toPath);
    },
    accountBalance(val) {
      if (Number(val) > 10000) {
        return `${(Number(val) / 10000).toFixed(1)}万`
      } else {
        return val
      }
    },
    clickRenewals(e) {
      //点击立即续费
      let toPath = '/member/vip/vipUp';
      this.iosAppRouteChange(toPath);
    },
    clickClassmates(e) {
      //点击同学
      let toPath = '/member/classmates/classmatesIndex';
      this.iosAppRouteChange(toPath);
    },
    clickOrder(e) {
      //点击订单
      let toPath = '/member/myOrder/allOrderList?orderType=-1';
      this.iosAppRouteChange(toPath);
    },
    clickScholarship(e) {
      //点击奖学金
      let toPath = '/member/scholarship/scholarshipIndex';
      this.iosAppRouteChange(toPath);
    },
    clickInterest(e) {
      //点击奖学金
      let toPath = '/member/myInterest/interestIndex';
      this.iosAppRouteChange(toPath);
    },
    clickGiftCard() {
      // 小程序环境不能跳充值页面
      if (this.$isWxApp()) {
        return
      }
      let toPath = '/member/giftCard/rechargeCard';
      this.iosAppRouteChange(toPath);
    },
    clickRecommend(e) {
      //点击推荐有礼
      let toPath = '/member/recommend/recommendCenter';
      this.iosAppRouteChange(toPath);
    },
    clickPartner(type) {
      //点击合伙人
      let toPath = type == 'shareholder' ? '/homePage/agent/agentIndex?fromType=shareholder' : '/homePage/agent/agentIndex';
      this.iosAppRouteChange(toPath);
    },
    clickHistory(e) {
      //点击观看记录
      let toPath = '/member/watchList/watchList?ckFrom=5';
      this.iosAppRouteChange(toPath);
    },
    clickCenter(upgradeMemberJump) {
      //点击会员中心
      let toPath = upgradeMemberJump == 4 ? "/member/vip/vipUp?type=23" : "/member/vip/vipUp";
      // let toPath = '/member/vip/vipUp';
      this.iosAppRouteChange(toPath);
    },
    clickIntegral(e) {
      //点击我的积分
      let toPath = '/member/myIntegral/integralDetail';
      this.iosAppRouteChange(toPath);
    },
    //点击报单系统
    clickForm() {
      if (!localStorage.getItem("uMobile")) {
        //let toPath = '/homePage/login/bindMobile';
        //this.iosAppRouteChange(toPath);
        if (utilJs.goBindMobile()) return;
      }
      let toPath = '/member/taxationForm/formIndex';
      this.iosAppRouteChange(toPath);
    },
    clickActive(e) {
      //点击我的活动
      let toPath = '/member/myActivity/myActivity';
      this.iosAppRouteChange(toPath);
    },
    clickTest(e) {
      //点击我的测评
      let toPath = '/member/myExamination/myExamination';
      this.iosAppRouteChange(toPath);
    },
    clickQA(e) {
      //点击我的问答
      localStorage.setItem("fromMemberIndex", 1);
      if (this.memberInfo.isAnswer == 1) {
        let toPath = '/homePage/qa/myQA?isAswer=1';
        this.iosAppRouteChange(toPath);
      } else {
        let toPath = '/homePage/qa/myQA';
        this.iosAppRouteChange(toPath);
      }
    },
    //我的优惠券
    clickMyValueVoucher(e) {
      localStorage.setItem("couponType", 2);
      let toPath = '/member/myValueVoucher/myValueVoucher';
      this.iosAppRouteChange(toPath);
    },
    //我的任务
    clickTask(e) {
      let toPath = '/member/myTask/myTask';
      this.iosAppRouteChange(toPath);
    },
    clickPresentCourse(e) {
      //点击我的赠送记录
      let toPath = '/member/presentCourse/presentCourseList';
      this.iosAppRouteChange(toPath);
    },
    clickCommunities(e) {
      //点击我的社群
      let toPath = '/member/mySocial/mySocialGroup';
      this.iosAppRouteChange(toPath);
    },
    clickCollageList(e) {
      //点击我的特惠
      let toPath = '/member/myCollage/myCollageList';
      this.iosAppRouteChange(toPath);
    },
    clickLiveList(e) {
      //点击我的直播
      let toPath = '/member/myLive/myLiveCourse';
      this.iosAppRouteChange(toPath);
    },
    clickMyExchange(e) {
      // 点击我的兑换
      let toPath = '/member/myExchange/exchangeIndex';
      this.iosAppRouteChange(toPath);
    },
    // 点击我的作业
    clickMyHomeWork(e) {
      let toPath = '/member/myClockIn/myClockInList';
      this.iosAppRouteChange(toPath);
    },
    // 点击我的小班课
    clickmyClassList(e) {
      let toPath = '/member/myPrivateClass/myClassList';
      this.iosAppRouteChange(toPath);
    },
    //点击我的练习
    clickmyQuestionBank(e) {
      let toPath = '/member/myPractice/myPracticeList';
      this.iosAppRouteChange(toPath);
    },
    // 点击我的成就
    clickMyAchievement(e) {
      let toPath = '/member/myAchievement/AchievementList';
      this.iosAppRouteChange(toPath);
    },
    // 点击我的错题
    clickWrongTopic(e) {
      let toPath = '/member/myWrongTopic/list';
      this.iosAppRouteChange(toPath);
    },
    // 点击我的投票
    clickMyVote(e) {
      let toPath = '/member/myVote/voteIndex';
      this.iosAppRouteChange(toPath);
    },
    // 点击我的砍价
    clickBargain(e) {
      let toPath = '/homePage/bargain/myBargain';
      this.iosAppRouteChange(toPath);
    },
    // 点击我的圈子
    clickCircle(e) {
      let toPath = '/homePage/circle/myCircle';
      this.iosAppRouteChange(toPath);
    },
    clickMyReservation(e) {
      if (this.companyAuth.enableDeclareModule == 1) { // 聚尚美跳到预约的主题列表
        let toPath = '/member/myReservation/myReservationList';
        this.iosAppRouteChange(toPath);
      } else {
        let toPath = '/member/myReservation/reservationClassList';
        this.iosAppRouteChange(toPath);
      }
    },
    // 点击词典
    clickMyWord(e) {
      let toPath = '/member/myWord/wordIndex';
      this.iosAppRouteChange(toPath);
    },
    // 点击预约
    clickMyBooking(e) {
      let toPath = '/member/myBooking/myBookingList';
      this.iosAppRouteChange(toPath);
    },
    clickNewAreaUser(e) { // 点击区域代理
      let comeFrom = !!this.companyAuth.enableNewAreaAgent && this.companyAuth.enableNewAreaAgent == 1 ? 0 : !!this.companyAuth.enableXhAreaAgent && this.companyAuth.enableXhAreaAgent == 1 ? 1 : '';
      let toPath = '/homePage/newAreaUser/apply?comeFrom=' + comeFrom;
      this.iosAppRouteChange(toPath);
    },
    // 面授课
    clickEdu(e) {
      // 当为1得时候跳转到学生端，为2跳转到老师端
      if (this.jwRole == 2) {
        let toPath = '/edu/courseManagment';
        this.iosAppRouteChange(toPath);
      } else {
        let toPath = '/edu/faceTeaching';
        this.iosAppRouteChange(toPath);
      }
    },
    // 待付款
    payments() {
      let toPath = '/member/myOrder/allOrderList?orderType=0';
      this.iosAppRouteChange(toPath);
    },
    // 待发货
    waitSend() {
      let toPath = '/member/myOrder/allOrderList?orderType=1';
      this.iosAppRouteChange(toPath);
    },
    // 待收货
    waitReceive() {
      let toPath = '/member/myOrder/allOrderList?orderType=2';
      this.iosAppRouteChange(toPath);
    },
    // 待评价
    waitAppraise() {
      let toPath = '/member/myOrder/allOrderList?orderType=3';
      this.iosAppRouteChange(toPath);
    },
    // 售后
    service () {
      let toPath = '/member/myRefund/myRefundList';
      this.iosAppRouteChange(toPath);
    },
    // 下载履约APP，也就是创客匠人学员学习中心app（有安卓，ios， 小程序）
    appDownload () {
      window.location.href = "https://kpreport.ckjr001.com/studyAppDownload.html";
    },
    // 秒 化成 小时
    formateH: function (time) {
      let min = 0;
      let h = 0;
      min = parseInt(time / 60);
      if (min > 59) {
        h = parseInt(min / 60);
      }
      return h
    },
    // 秒 化成 分
    formateM: function (time) {
      let min = 0;
      min = parseInt(time / 60);
      if (min > 59) {
        min = parseInt(min % 60);
        return min;
      }
      return min;
    },
    computerServerName(val) {
      if (val.length > 5) {
        return `${val.substr(0, 5)}...`
      } else {
        return val
      }
    },
    clickMyCollection(e) {
      let toPath = '/member/myCollection/myCollection';
      this.iosAppRouteChange(toPath);
    },
    clickMyShopInfo() {
      let toPath = '/member/myShopInfo/imgVerify';
      this.iosAppRouteChange(toPath);
    },
    countNameLength(val) {
      if (val.length > 9) {
        return `${val.substr(0, 9)}...`
      } else {
        return val;
      }
    },
    mx_getShopDetail() {
      utilJs.getMethod(`${global.apiurl}company/shopDetail`, (res) => {
        this.showShopDetail = JSON.stringify(res) != '[]' ? true : false;
      });
    },
    mx_initData() {
      const TEXT_CHANGE = ['3qxm', 'lao4', 'ygzw'];
      if (TEXT_CHANGE.includes(localStorage.getItem("companyId"))) {
        this.exchangeText = '我的兑换';
      } else {
        this.exchangeText = '优惠券';
      }
      this.isAnApp = utilJs.isAndroidApp() || utilJs.isAndoridNewApp();
      this.isiOS = utilJs.isIOSAppALL();
      this.companyAuth = localStorage.getItem("companyAuth") ? JSON.parse(localStorage.getItem("companyAuth")) : null;
      this.isYouke = utilJs.isIOSAnonUser();
      this.isApp = utilJs.isAndroidApp() || utilJs.isIOSApp();
    },
    //获取个人中心模块信息
    mx_getMemberIndexModuleInfo() {
      utilJs.getMethod(`${global.apiurl}members/getPersonalConf`, (res) => {
        this.modeleMapInfo = res;
        this.baseConf = res.baseConf;
        if (res.personalModuleList.filter(item => item.type == 6).length > 0) {
          let item = res.personalModuleList.filter(item => item.type == 6)[0];
          let list = res.personalModuleList.filter(item => item.type != 6);
          list.unshift(item);
          res.personalModuleList = list;
          this.assetManagement = item;
          if (this.assetManagement.project && this.assetManagement.project.filter(item => item.enable == 0).length == this.assetManagement.project.length) {
            this.$set(this.assetManagement, 'status', 0)
          }
        }
        this.personalModuleList = res.personalModuleList;
        this.isShowStudy = res.personalModuleList.find(item => item.type == 2 && item.status == 1) ? true : false;
        this.isShowOrder = res.personalModuleList.find(item => item.type == 1 && item.status == 1) ? true : false;
        if (res.enableReleaseCloud == 1) {
          let tempArr = res.personalModuleList.find(item => item.type == 5);
          tempArr.project.push({
            defaultName: "发售邀约",
            enable: 1,
            // id: 117,
            keyword: "RELEASE_CLOUD",
            name: "发售邀约",
            personalModule: 5,
            sort: tempArr.project.length + 1
          })
          tempArr.isEmptyProject = tempArr.project.length;
          const index = res.personalModuleList.findIndex(i => i.type == 5);
          this.personalModuleList[index] = tempArr;
        }

        this.promotedAccount = res.promotedAccount;
        // if (Array.isArray(res)) {
        //   let infoObj = {};
        //   for (let item of res) {
        //     item.name = item.name || item.defaultName;
        //     infoObj[item.keyword] = item;
        //   }
        //   this.modeleMapInfo = infoObj;
        // }
      });
    },
    mx_getCardName() {
      utilJs.getGiftCardInfo().then(res => {
        this.giftCardName = res.name || '虚拟币';
      })
    },
    mx_getMemberInfo() {
      utilJs.getMethod(`${global.apiurl}members`, res => {
        //张法老师定制，服务商以下身份的，隐藏奖学金和收入
        if (localStorage.getItem('hideHead') == 'true' && (res.vipType == 1 || res.vipType == 2 || res.vipType == 4)) {
          this.hideRecord = true;
        } else {
          this.hideRecord = false;
        }
        if (res.userId && isNaN(res.userId)) {
          localStorage.setItem("userId", res.userId);
        }
        localStorage.setItem(`vipType${localStorage.getItem('userId')}`, res.vipType) // 重新把身份写入缓存，区域代理那边用到
        this.jwRole = res.jwRole.role;
        this.vipTypeNum = res.vipType;
        this.svipEndTime = res.svipEndTime ? res.svipEndTime.slice(0, 10) : '';
        this.vipEndTime = res.vipEndTime ? res.vipEndTime.slice(0, 10) : '';
        this.balance = res.balance;
        this.questionInfo = res.questionInfo;
        this.recId = res.recId;
        this.recDec = res.actionDec;
        this.companyAuth = JSON.parse(localStorage.getItem("companyAuth"));
        this.isShowSign = res.enablePoints == 1;
        this.enableIosRecharge = (res.enableIosRecharge == 1 && this.isiOS) || !this.isiOS; //ios设备&&开启充值 || 非ios设备
        this.isShowRenew = ((res.day > 0 && res.day < 7 && this.vipTypeNum == 2) || (((res.vipDay > 0 && res.vipDay < 7) || (res.vipDay == 0 && res.day > 0 && res.day < 7)) && this.vipTypeNum == 4)) //res.day > 0 && res.day < 7;
        this.memberInfo = res;
        let nameMap = utilJs.getCustNameMap();
        if (res.vipType == 3 && res.hasSubType == 1 && res.subTypeName && res.subTypeName.length > 0) {
          res.subType = res.subType ? res.subType : 0;
          let tItem = res.subTypeName.filter((item) => {
            return item.subType == res.subType;
          })
          if (tItem && tItem.length > 0) {
            this.vipType = tItem[0].name;
          }
        } else {
          this.vipType = nameMap[res.vipType] || '';
        }
        utilJs.getGiftCardInfo().then(res => {
          this.enableGiftCard = !!Number(res.status) && !utilJs.isAndroidApp() && !utilJs.isIOSApp();
        })
        this.showPresent = res.addMemberGift ? true : false; // 是否展示会员礼包
        this.addMemberGift = res.addMemberGift; // 会员礼包详情
      })
    },
    //关闭升级&推荐礼包
    closePresent() {
      this.showPresent = false;
    },
  },
};
